﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using QLTV.Database;
using DevExpress.XtraTreeList.Nodes.Operations;
using DevExpress.XtraTreeList;

namespace QLTV.Forms.KhaiBao
{
    public partial class frmDiaChi : BaseForm
    {
        public frmDiaChi()
        {
            InitializeComponent();
            createTree();
        }

        private void createTree()
        {
            AddressDataContext data = new AddressDataContext();
            var listTinhThanh = from t in data.tblTinhThanhs
                                select t;

            diaChiTreeList.Nodes.Clear();

            diaChiTreeList.BeginUpdate();
            diaChiTreeList.Columns.Add();
            diaChiTreeList.Columns[0].Caption = "Name";
            diaChiTreeList.Columns[0].VisibleIndex = 0;

            diaChiTreeList.Columns.Add();
            diaChiTreeList.Columns[1].Caption = "Id";
            diaChiTreeList.Columns[1].VisibleIndex = 1;
            diaChiTreeList.Columns[1].Visible = false;
            diaChiTreeList.EndUpdate();

            DevExpress.XtraTreeList.Nodes.TreeListNode parentNode = null;

            foreach (tblTinhThanh tinhThanh in listTinhThanh)
            {
                DevExpress.XtraTreeList.Nodes.TreeListNode rootNode = diaChiTreeList.AppendNode(new object[] { tinhThanh.TenTinhThanh, tinhThanh.TinhThanhID }, parentNode);

                var listQuanHuyen = from q in data.tblQuanHuyens
                                    where q.TinhThanhID == tinhThanh.TinhThanhID
                                    select q;

                foreach (tblQuanHuyen quanHuyen in listQuanHuyen)
                {
                    DevExpress.XtraTreeList.Nodes.TreeListNode childNode = diaChiTreeList.AppendNode(new object[] { quanHuyen.TenQuanHuyen, quanHuyen.QuanHuyenID }, rootNode);

                    var listPhuongXa = from p in data.tblPhuongXas
                                       where p.QuanHuyenID == quanHuyen.QuanHuyenID
                                       select p;
                    foreach (tblPhuongXa phuongxa in listPhuongXa)
                    {
                        DevExpress.XtraTreeList.Nodes.TreeListNode childNode1 = diaChiTreeList.AppendNode(new object[] { phuongxa.TenPhuongXa, phuongxa.PhuongXaID }, childNode);
                    }
                }
            }

            diaChiTreeList.ExpandAll();
        }

        private void diaChiTreeList_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                tinhThanhPopupMenu.ShowPopup(new Point(Cursor.Position.X, Cursor.Position.Y));
            }
        }

        private void fillpopup(int level)
        {
            if (this.node != null)
            {
                switch (this.node.Level)
                {
                    case 0:
                        frmTinhThanh tinhthanh = new frmTinhThanh();
                        tinhthanh.StartPosition = FormStartPosition.CenterScreen;
                        tinhthanh.ShowDialog();
                        createTree();
                        break;
                    case 1:
                        frmQuanHuyen quanhuyen = new frmQuanHuyen();
                        quanhuyen.StartPosition = FormStartPosition.CenterScreen;
                        quanhuyen.TinhThanhID = Convert.ToInt32(node.ParentNode.GetValue(this.diaChiTreeList.Columns[1]));

                        quanhuyen.ShowDialog();
                        createTree();
                        break;
                    case 2:
                        frmPhuongXa phuongxa = new frmPhuongXa();
                        phuongxa.StartPosition = FormStartPosition.CenterScreen;
                        phuongxa.PhuongXaId = int.Parse(this.node.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        phuongxa.QuanHuyenId = int.Parse(this.node.ParentNode.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        phuongxa.TinhThanhId = int.Parse(this.node.ParentNode.ParentNode.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        phuongxa.ShowDialog();
                        createTree();
                        break;
                    default:
                        break;
                }
            }
        }


        private void btnThemTinhThanh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmTinhThanh f = new frmTinhThanh();
            f.ShowDialog();
            createTree();
        }

        private void btnThemQuanHuyen_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmQuanHuyen f = new frmQuanHuyen();

            //if (this.node != null)
            //{
            //    switch (this.node.Level)
            //    {
            //        case 0:
            //            f.TinhThanhId = int.Parse(this.node.GetValue(this.diaChiTreeList.Columns[1]).ToString());
            //            break;
            //        case 1:
            //            f.TinhThanhId = int.Parse(this.node.ParentNode.GetValue(this.diaChiTreeList.Columns[1]).ToString());
            //            f.QuanHuyenId = int.Parse(this.node.GetValue(this.diaChiTreeList.Columns[1]).ToString());
            //            break;
            //        case 2:
            //            f.TinhThanhId = int.Parse(this.node.ParentNode.ParentNode.GetValue(this.diaChiTreeList.Columns[1]).ToString());
            //            f.QuanHuyenId = int.Parse(this.node.ParentNode.GetValue(this.diaChiTreeList.Columns[1]).ToString());
            //            break;
            //        default:
            //            break;
            //    }
            //}

            if (this.node.ParentNode != null)
            {
                f.TinhThanhID = Convert.ToInt32(node.ParentNode.GetValue(this.diaChiTreeList.Columns[1]));
            }
            f.ShowDialog();

            createTree();
        }

        private void btnThemPhuongXa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmPhuongXa f = new frmPhuongXa();

            if (this.node != null)
            {
                switch (this.node.Level)
                {
                    case 0:
                        f.TinhThanhId = int.Parse(this.node.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        break;
                    case 1:
                        f.TinhThanhId = int.Parse(this.node.ParentNode.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        f.QuanHuyenId = int.Parse(this.node.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        break;
                    case 2:
                        //f.PhuongXaId = int.Parse(this.node.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        f.QuanHuyenId = int.Parse(this.node.ParentNode.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        f.TinhThanhId = int.Parse(this.node.ParentNode.ParentNode.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        break;
                    default:
                        break;
                }
            }

            f.ShowDialog();
            createTree();
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.node != null)
            {
                switch (this.node.Level)
                {
                    case 0:
                        frmTinhThanh tinhthanh = new frmTinhThanh();
                        tinhthanh.StartPosition = FormStartPosition.CenterScreen;
                        tinhthanh.TinhThanhId = int.Parse(this.node.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        tinhthanh.ShowDialog();
                        createTree();
                        break;
                    case 1:
                        frmQuanHuyen quanhuyen = new frmQuanHuyen();
                        quanhuyen.StartPosition = FormStartPosition.CenterScreen;
                        quanhuyen.QuanHuyenId = int.Parse(this.node.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        quanhuyen.ShowDialog();
                        createTree();
                        break;
                    case 2:
                        frmPhuongXa phuongxa = new frmPhuongXa();
                        phuongxa.StartPosition = FormStartPosition.CenterScreen;
                        phuongxa.PhuongXaId = int.Parse(this.node.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        phuongxa.QuanHuyenId = int.Parse(this.node.ParentNode.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        phuongxa.TinhThanhId = int.Parse(this.node.ParentNode.ParentNode.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        phuongxa.ShowDialog();
                        createTree();
                        break;
                    default:
                        break;
                }
            }
        }

        private void diaChiTreeList_FocusedNodeChanged(object sender, FocusedNodeChangedEventArgs e)
        {
            node = e.Node;

            if (node != null && node.Level == 2)
            {
                btnChoice.Enabled = true;
            }
            else
                btnChoice.Enabled = false;
        }

        private DevExpress.XtraTreeList.Nodes.TreeListNode node;

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChoice_Click(object sender, EventArgs e)
        {
            if (this.node != null)
            {
                switch (this.node.Level)
                {
                    case 0:
                        int tinhthanhid = int.Parse(this.node.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        ListId = new List<int>();
                        ListId.Add(tinhthanhid);
                        break;
                    case 1:
                        int tinhthanhid1 = int.Parse(this.node.ParentNode.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        int quanhuyenid = int.Parse(this.node.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        ListId = new List<int>();
                        ListId.Add(tinhthanhid1);
                        ListId.Add(quanhuyenid);
                        break;
                    case 2:
                        int phuongxa = int.Parse(this.node.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        int quanhuyen = int.Parse(this.node.ParentNode.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        int tinhthanh = int.Parse(this.node.ParentNode.ParentNode.GetValue(this.diaChiTreeList.Columns[1]).ToString());
                        ListId = new List<int>();
                        ListId.Add(tinhthanh);
                        ListId.Add(quanhuyen);
                        ListId.Add(phuongxa);
                        break;
                    default:
                        break;
                }
            }

            this.Close();
        }

        public List<int> ListId
        {
            get;
            set;
        }
    }
}